package icu.yougan.trade.utils;

import icu.yougan.trade.entity.IdleItem;
import icu.yougan.trade.entity.Order;
import icu.yougan.trade.service.IdleItemService;
import icu.yougan.trade.service.OrderService;
import jakarta.annotation.Resource;

import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

// 新建一个定时任务类
@Component
public class OrderExpireTask {
    @Resource
    private OrderService orderService;
    @Resource
    private IdleItemService idleItemService;
    @Scheduled(fixedRate = 60000) // 每分钟检查一次
    public void checkExpiredOrders() {
        List<Order> expiredOrders = orderService.getExpiredOrders(new Date());
        expiredOrders.forEach(order -> {
            order.setOrderStatus((byte) 4); // 设置为已取消
            IdleItem idleItem=new IdleItem();
            idleItem.setId(order.getIdleId());
            idleItem.setIdleStatus((byte) 1);
            idleItemService.updateIdleItem(idleItem);
            orderService.updateOrder(order);
        });
    }
}